/*
 * @Register.java	2008/07/28
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.lightweightlog;

import java.util.LinkedList;
import java.util.Properties;

/**
 * Where all our Dumper will be stored, all extending classes are asked to implement few methods.
 */
abstract class Register<T extends Dumper> {
    
    /**
     * The status of this Register
     */
    public enum Status {
        INIT,
        OPEN,
        FULL,
        FAILURE,
    }
     
    /**
     * Constructor
     * @param p
     * @param d
     */
    public Register(Properties p, T... t) {    	
    	this.init(p,t);
    }
    
    /**
     * Implemented by extended class
     */
    void preinit() {    	
    }
    
    /**
     * Initialization block
     * @param p set of Properties
     * @param t Array of Dumper
     */
    private void init(Properties p, T... t) {

        // check if we have passed a null Array
        if (t.length < 1) {
            throw new ObjectInitializationException("Dumper array:" + t.length);        // calling default constructor
        }

        // calling preinit...
        this.preinit();
        
        // browse our dumper array
        // and store in register
        dumper:
        for (int i = 0; i < t.length; i++) {
            // check if our dumper is null
            if (t[i] == null) {
                Log.serviceLog(i + " is null");
                continue dumper;
            }

            if (!this.save(t[i])) {
                Log.serviceLog("Couldn't save " + t.toString());
            }
        }
        
        // calling if needed...
        this.postinit();
    }
    
    void postinit() {    	
    }
    
    /**
     * Size of this register
     * @return int
     */
    abstract int size();
    
    /**
     * Save dumper to slot
     * @param t extended Dumper
     * @return true if this Dumper is properly saved
     */
    abstract boolean save(T t);

    /**
     * Remove dumper from slot
     * @param t extended Dumper
     * @return true if this Dumper is properly saved
     */
    abstract boolean remove(T t);
    
    /**
     * Dispatch a list of entry to each dumper in this register
     * @param ll A LinkedList of Entry
     */
    abstract <E extends Entry> void dispatchToDumper(LinkedList<E> ll);
    
    /**
     * Dispatch an entry to each dumper in this register
     * @param e Any extended Entry
     */
    abstract <E extends Entry> void dispatchToDumper(E e);
}